<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryFormRef" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="规格编码" prop="specCode">
        <el-input
          v-model="queryParams.specCode"
          placeholder="请输入规格编码"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="商品编码" prop="goodsCode">
        <el-input
          v-model="queryParams.goodsCode"
          placeholder="请输入商品编码"
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item label="状态 " prop="state">
        <el-input
          v-model="queryParams.state"
          placeholder="请输入状态 "
          clearable
          size="small"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['product:goodsSpec:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['product:goodsSpec:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['product:goodsSpec:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['product:goodsSpec:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="goodsSpecList"
      :default-sort="tableSort"
      @sort-change="handleSortChange"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="规格编码" align="center" prop="specCode" />
      <el-table-column label="商品编码" align="center" prop="goodsCode" />
      <el-table-column label="售价" align="center" prop="salePrice" />
      <el-table-column label="规格一值" align="center" prop="firstSpecValue" />
      <el-table-column label="规格二值" align="center" prop="secondSpecValue" />
      <el-table-column label="主图" align="center" prop="mainImg" />
      <el-table-column label="状态 " align="center" prop="state" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180" sortable="custom">
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['product:goodsSpec:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['product:goodsSpec:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      :page="queryParams.pageNum"
      :limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改商品规格对话框 -->
    <el-dialog v-model="dialogVisible" :title="dialogTitle" width="500px" append-to-body  >
      <el-form ref="formRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="规格编码" prop="specCode">
          <el-input v-model="form.specCode" placeholder="请输入规格编码" />
        </el-form-item>
        <el-form-item label="市场价" prop="marketPrice">
          <el-input v-model="form.marketPrice" placeholder="请输入市场价" />
        </el-form-item>
        <el-form-item label="售价" prop="salePrice">
          <el-input v-model="form.salePrice" placeholder="请输入售价" />
        </el-form-item>
        <el-form-item label="VIP价" prop="vipPrice">
          <el-input v-model="form.vipPrice" placeholder="请输入VIP价" />
        </el-form-item>
        <el-form-item label="成本价" prop="costPrice">
          <el-input v-model="form.costPrice" placeholder="请输入成本价" />
        </el-form-item>
        <el-form-item label="库存" prop="stockNum">
          <el-input v-model="form.stockNum" placeholder="请输入库存" />
        </el-form-item>
        <el-form-item label="销量" prop="saleNum">
          <el-input v-model="form.saleNum" placeholder="请输入销量" />
        </el-form-item>
        <el-form-item label="排序" prop="sortNum">
          <el-input v-model="form.sortNum" placeholder="请输入排序" />
        </el-form-item>
        <el-form-item label="规格一值" prop="firstSpecValue">
          <el-input v-model="form.firstSpecValue" placeholder="请输入规格一值" />
        </el-form-item>
        <el-form-item label="规格二值" prop="secondSpecValue">
          <el-input v-model="form.secondSpecValue" placeholder="请输入规格二值" />
        </el-form-item>
        <el-form-item label="主图">
          <imageUpload v-model="form.mainImg"/>
        </el-form-item>
        <el-form-item label="轮播图">
          <imageUpload v-model="form.bannerImg"/>
        </el-form-item>
        <el-form-item label="状态 " prop="state">
          <el-input v-model="form.state" placeholder="请输入状态 " />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" placeholder="请输入备注" />
        </el-form-item>
      </el-form>
      <template #footer>
          <div class="dialog-footer">
            <el-button @click="handleDialogCancel">取 消</el-button>
            <el-button type="primary" @click="submitForm">确 定</el-button>
          </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="GoodsSpec">
import { ref, toRefs } from "vue";
import { listGoodsSpec, getGoodsSpec, delGoodsSpec, addGoodsSpec, updateGoodsSpec, exportXlsUrl } from "@/api/product/goodsSpec";

const { proxy } = getCurrentInstance();

  // 遮罩层
  const loading = ref(true);
  // 选中数组
  const ids = ref([]);
  // 非单个禁用
  const single = ref(true);
  // 非多个禁用
  const multiple = ref(true);
  // 显示搜索条件
  const showSearch = ref(true);
  // 总条数
  const total = ref(0);
  // 商品规格表格数据
  const goodsSpecList  = ref([]);
  // 弹出层标题
  const dialogTitle  = ref("");
  // 是否显示弹出层
  const dialogVisible  = ref(false);
  // 查询参数
     const reactiveData  = reactive(
     {
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          specCode: null,
          goodsCode: null,
          state: null,
            params: {
              sort: 'createTime',
              order: 'descending',
            }
        },
        form: {},
        rules: {
        goodsId: [
          { required: true, message: "商品id不能为空", trigger: "blur" }
        ],
        specCode: [
          { required: true, message: "规格编码不能为空", trigger: "blur" }
        ],
        goodsCode: [
          { required: true, message: "商品编码不能为空", trigger: "blur" }
        ],
        marketPrice: [
          { required: true, message: "市场价不能为空", trigger: "blur" }
        ],
        salePrice: [
          { required: true, message: "售价不能为空", trigger: "blur" }
        ],
        vipPrice: [
          { required: true, message: "VIP价不能为空", trigger: "blur" }
        ],
        costPrice: [
          { required: true, message: "成本价不能为空", trigger: "blur" }
        ],
        stockNum: [
          { required: true, message: "库存不能为空", trigger: "blur" }
        ],
        saleNum: [
          { required: true, message: "销量不能为空", trigger: "blur" }
        ],
        sortNum: [
          { required: true, message: "排序不能为空", trigger: "blur" }
        ],
        firstSpecValue: [
          { required: true, message: "规格一值不能为空", trigger: "blur" }
        ],
        secondSpecValue: [
          { required: true, message: "规格二值不能为空", trigger: "blur" }
        ],
        mainImg: [
          { required: true, message: "主图不能为空", trigger: "blur" }
        ],
        bannerImg: [
          { required: true, message: "轮播图不能为空", trigger: "blur" }
        ],
        state: [
          { required: true, message: "状态 不能为空", trigger: "blur" }
        ],
        remark: [
          { required: true, message: "备注不能为空", trigger: "blur" }
        ],
        createBy: [
          { required: true, message: "创建人不能为空", trigger: "blur" }
        ],
        updateBy: [
          { required: true, message: "修改人不能为空", trigger: "blur" }
        ],
        yn: [
          { required: true, message: "0无效1有效不能为空", trigger: "blur" }
        ]
        }
     });

      const {queryParams,form,rules} = toRefs(reactiveData);

      const tableSort = computed(()=>{
        if(queryParams.value.params.sort){
          return {
            prop: queryParams.value.params.sort,
            order: queryParams.value.params.order
          }
        }
        return null;
      })

    /** 查询商品规格列表 */
   function getList() {
     loading.value = true;
      listGoodsSpec(queryParams.value).then(response => {
        goodsSpecList.value = response.data.records;
        total.value = response.data.total;
        loading.value = false;
      });
    }
    // 取消按钮
    function handleDialogCancel() {
      dialogVisible.value = false;
      reset();
    }
    // 表单重置
    function reset() {
      form.value = {
        id: null,
        goodsId: null,
        specCode: null,
        goodsCode: null,
        marketPrice: null,
        salePrice: null,
        vipPrice: null,
        costPrice: null,
        stockNum: null,
        saleNum: null,
        sortNum: null,
        firstSpecValue: null,
        secondSpecValue: null,
        mainImg: null,
        bannerImg: null,
        state: null,
        remark: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        yn: null
      };
      proxy.resetForm("formRef");
    }
    /** 搜索按钮操作 */
   function handleQuery() {
      queryParams.value.pageNum = 1;
      getList();
    }
    /** 重置按钮操作 */
    function resetQuery() {
      proxy.resetForm("queryFormRef");
      handleQuery();
    }

    // 多选框选中数据
    function handleSelectionChange(selection) {
      ids.value = selection.map(item => item.id)
      single.value = selection.length!==1
      multiple.value = !selection.length
    }

    // 排序
    function handleSortChange({ column, prop, order }){
      queryParams.value.params.sort = prop?prop:null;
      queryParams.value.params.order = order?order:null;
      getList();
    }

    /** 新增按钮操作 */
    function handleAdd() {
      reset();
      dialogVisible.value = true;
      dialogTitle.value = "添加商品规格";
    }

    /** 修改按钮操作 */
    function handleUpdate(row) {
      reset();
      const id = row.id || ids.value
      getGoodsSpec(id).then(response => {
        form.value = response.data;
        dialogVisible.value = true;
        dialogTitle.value = "修改商品规格";
      });
    }
    /** 提交按钮 */
    function submitForm() {
      proxy.$refs["formRef"].validate(valid => {
        if (valid) {
          if (form.value.id != null) {
            updateGoodsSpec(form.value).then(response => {
              proxy.$modal.msgSuccess("修改成功");
              dialogVisible.value = false;
              getList();
            });
          } else {
            addGoodsSpec(form.value).then(response => {
              proxy.$modal.msgSuccess("新增成功");
              dialogVisible.value = false;
              getList();
            });
          }
        }
      });
    }
    /** 删除按钮操作 */
   function handleDelete(row) {
      const ids = row.id || ids.value;
      proxy.$modal.confirm('是否确认删除商品规格编号为"' + ids + '"的数据项？').then(function() {
        return delGoodsSpec(ids);
      }).then(() => {
        getList();
        proxy.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    }
   /** 导出按钮操作 */
   function handleExport() {
      proxy.download(exportXlsUrl, {
        ...queryParams.value
      }, `goodsSpec_${new Date().getTime()}.xlsx`)
    }
   /** 初始化数据 **/
   getList();
</script>
